package com.wswy.wzcx.ui.activity;

import android.content.Context;
import android.content.Intent;

import com.che.libcommon.ui.BaseFragment;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.resp.CaptchaData;
import com.wswy.wzcx.module.validator.IValidator;
import com.wswy.wzcx.module.validator.TCaptchaValidator;
import com.wswy.wzcx.ui.fragment.ChangePwdFragment;
import com.wswy.wzcx.ui.fragment.PreChangePwdFragment;
import com.wswy.wzcx.ui.fragment.PreSetupPwdFragment;
import com.wswy.wzcx.ui.fragment.SetupPwdFragment;

/**
 * Created by wangduheng26 on 2018/1/22.
 * wangduheng26@gmail.com
 */

public class PwdActivity extends IFragmentActivity implements ITestValidator {

    public static Intent asIntent(Context context, String target) {
        Intent intent = new Intent(context, PwdActivity.class);
        intent.putExtra(EXTRA_TARGET_NAME, target);
        return intent;
    }

    private IValidator validator;


    @Override
    protected BaseFragment getFragmentFromExtra(String stringExtra) {
        switch (stringExtra) {
            case PreSetupPwdFragment.TAG:
                return PreSetupPwdFragment.newInstance();
            case SetupPwdFragment.TAG:
                return SetupPwdFragment.newInstance("","");
            case PreChangePwdFragment.TAG:
                return PreChangePwdFragment.newInstance();
            case ChangePwdFragment.TAG:
                return ChangePwdFragment.newInstance("");

        }
        return null;
    }


    public void replaceFragment(String stringExtra) {
        BaseFragment extra = getFragmentFromExtra(stringExtra);
        replaceFragment(extra);
    }

    public void replaceFragment(BaseFragment extra) {
        if (extra != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, extra).commitAllowingStateLoss();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroy();
    }

    @Override
    public IValidator getValidator(CaptchaData data) {
        if (validator == null){
            validator = new TCaptchaValidator(this);
        }
        return validator;
    }

    @Override
    public void destroy() {
        if (validator != null){
            validator.onDestroy();
        }
    }
}
